import {Component, OnInit, ViewChild} from '@angular/core';
import {Router} from '@angular/router';
import {ModalDirective} from 'ngx-bootstrap';
import {PermissionService} from '../../../lib/service/tuike/permission.service';
import {AddPermission, EditPermission, PermissionInfo} from './permission';

@Component({
  selector: 'app-permission',
  templateUrl: './permission.component.html',
  styleUrls: ['./permission.component.scss'],
  providers: [PermissionService]
})
export class PermissionComponent implements OnInit {

  totalItems: number;
  currentPage = 1;
  itemsPerPage = 12;
  name: string;
  type: string;
  message: string;
  isShow: boolean = false;
  delId: number;
  permissionList = [];
  addPermission = new AddPermission();
  editPermission = new EditPermission();
  editItem = null;
  editPermissionInfo;
  permissionInfo;
  editPermissionLevel = 0;
  permissionTree = [];
  permissionOptions = [];
  checkedParentId;
  _isSpinning = false;
  nameError = false;

  @ViewChild('addModal') public addModal: ModalDirective;
  @ViewChild('editModal') public editModal: ModalDirective;
  @ViewChild('delModal') public delModal: ModalDirective;
  @ViewChild('addSonModal') public addSonModal: ModalDirective;
  @ViewChild('treeModal') public treeModal: ModalDirective;


  constructor(
    private router: Router,
    private permissionService: PermissionService
  ) {

  }

  ngOnInit() {
    this.getPermissionList();
    this.getPermissionTree();
  }

  getPermissionList() {
    this._isSpinning = true;
    this.permissionService.getPermissionList(this.currentPage, this.itemsPerPage).subscribe(
      data => {
        console.log(data);
        if (data.code === 200) {
          this.permissionList = data.data.data;
          this.totalItems = data.data.total;
          this._isSpinning = false;
        }
      }
    );
  }

  getPermissionTree() {
    this.permissionTree = [];
    this.permissionService.getPermissionTree().subscribe(res => {
      if (res.code === 200) {
        let data = res.data;
        this.permissionTree.push({
          level: data.permission_level,
          label: data.permission_name,
          id: data.id,
          parent_id: -1
        });
        this.addPermission.parent_id = -1;
        this.renderOptions(res.data.children);
      }
    })
  }

  renderOptions(arr,parentName?) {
    arr.map((e, i) => {
      console.log(e);
      let split = '|';
      for(var i1 = 0; i1<=e.permission_level;i1++) {
        split += '___';
      };
      this.permissionTree.push({
        level: e.permission_level,
        label: e.permission_name,
        parent_id: e.parent_id,
        id: e.id,
        split:split,
        parent_name:parentName? parentName :''
      });
      if (e.children) {
        this.renderOptions(e.children,e.permission_name);
      }
    });
  }

  showDetailModal() {

  }

  log(e) {
    console.log('selected', e);
  }

  showTreeModal() {
    this.treeModal.show();
  }

  showAddModal(item?) {
    if (item) {
      this.getPermissionInfo(item.id);
    } else {
      this.addPermission.permission_level = this.permissionTree[0]['permission_level'];
      this.addPermission.permission_name = '';
      this.addPermission.parent_id = -1;
      this.addPermission.action = '';
    }
    this.addModal.show();
  }


  showEditModal(item) {
    this.editModal.show();
    this.editPermission.id = item.id;
    this.getPermissionInfo(item.id, true);
  }

  showDelModal(id) {
    this.delId = id;
    this.delModal.show();
  }

  showAddSonModal(item) {
    this.editItem = item;
    this.addSonModal.show();
  }

  getPermissionInfo(id, flag?) {
    this.permissionService.getPermissionInfo(id).subscribe(
      data => {
        if (data.code === 200) {
          this.permissionInfo = data.data;
          if(flag) {
            if(this.permissionInfo.parent_permission) {
              console.log(this.permissionInfo.parent_permission);
              this.editPermission.permission_level = this.permissionInfo.parent_permission.permission_level;
              this.editPermission.permission_name = this.permissionInfo.permission_name;
              this.editPermission.parent_id = this.permissionInfo.parent_permission.id;
              this.editPermission.action = this.permissionInfo.action;
            } else {
              this.editPermission.permission_level = this.permissionTree[0]['permission_level'];
              this.editPermission.permission_name = this.permissionInfo.permission_name;
              this.editPermission.parent_id = -1;
              this.editPermission.action = this.permissionInfo.action;
              }
          } else {
            this.addPermission.permission_level = this.permissionInfo.permission_level;
            this.addPermission.parent_id = this.permissionInfo.id;
          }
        }
      }
    );
  }

  checkLength(e) {
    if(e.length < 3) {
      this.nameError = true;
    } else {
      this.nameError = false;
    }
  }

  checked_level = null;

  checkedLevel(arr, parentId) {
    arr.map((e, i) => {
      if (e['parent_id'] == parentId) {
        this.checked_level = e['permission_level'];
        return;
      } else {
        this.checkedLevel(e.children, parentId);
      }
    })
  }

  addPermissionSubmit() {
    let level = null;
    this.permissionTree.map((e, i) => {
      if (e['id'] == this.addPermission.parent_id) {
        level = e['level'];
      }
    });
    this.addPermission.permission_level = (this.addPermission.parent_id == 0 || this.addPermission.parent_id == -1) ? 0 : parseInt(level) + 1;
    this.addPermission.parent_id = (this.addPermission.parent_id == 0 || this.addPermission.parent_id == -1) ? 0 : this.addPermission.parent_id;
    this.permissionService.addPermission(this.addPermission).subscribe(
      data => {
        if (data.code === 200) {
          this.type = 'success';
          this.message = '添加成功';
          this.isShow = true;
          this.addModal.hide();
          this.addPermission = new AddPermission();
          this.getPermissionTree();
          this.getPermissionList();
        }
      }
    );
  }

  addSonPermissionSubmit() {

  }

  editPermissionSubmit() {
    let level = null;
    this.permissionTree.map((e, i) => {
      if (e['id'] == this.editPermission.parent_id) {
        level = e['level'];
      }
    });
    this.editPermission.permission_level = (this.editPermission.parent_id == 0 || this.editPermission.parent_id == -1) ? 0 : parseInt(level) + 1;
    this.editPermission.parent_id = (this.editPermission.parent_id == 0 || this.editPermission.parent_id == -1) ? 0 : this.editPermission.parent_id;
    this.permissionService.editPermission(this.editPermission).subscribe(
      data => {
        if (data.code === 200) {
          this.type = 'success';
          this.message = '修改成功';
          this.isShow = true;
          this.editModal.hide();
          this.getPermissionTree();
          this.getPermissionList();
        }
      }
    );
  }

  delPermission() {
    this.permissionService.delPermission(this.delId).subscribe(
      data => {
        if (data.code === 200) {
          this.type = 'success';
          this.message = '删除成功';
          this.isShow = true;
          this.delModal.hide();
          this.getPermissionList();
        }
      }
    );
  }


  /*pageChanged(event: any): void {

  }*/

  pageChanged(event: any): void {
    window.scrollTo(0, 0);
    this.currentPage = event.page;
    this.getPermissionList();
  }

  promptIsShow(event: any): void {
    this.isShow = event;
  }
}
